from django.shortcuts import render
from libs.captcha.captcha import captcha
from rest_framework.views import APIView
from django_redis import get_redis_connection
from . import constants
from django.http import HttpResponse
from rest_framework.generics import GenericAPIView
from .serializers import RegisterSmsCodeSerializer
from random import randint
from rest_framework.response import Response
from rest_framework import status
from libs.yuntongxun.sms import CCP
from rest_framework.generics import CreateAPIView

# Create your views here.
# 验证码 和 短信验证码的 2个接口

# APIView
#GenericAPIView
#ListAPIView,CreateAPIView

# 因为我没有用到序列化器,所以继承自APIView
class RegisterImageCodeView(APIView):

    """
    GET /verifications/imagecodes/(?P<image_code_id>.+)/
    image_code_id 是作为 判断 用户 输入的时候 查询 redis的依据
    image_code_id js生成的


    """


    def get(self,request,image_code_id):

        # 1. 生成验证码和验证码图片
        text,image = captcha.generate_captcha()

        # 2. 保存验证码到redis中
        # 传入 settings中的一个别名就可以
        # ctrl+shift+t 打开cmd的在同一个窗口打开新的
        redis_conn = get_redis_connection('code')

        # redis_conn.setex('img_%s'%image_code_id,60,text)
        redis_conn.setex('img_%s'%image_code_id,constants.IMAGE_EXPIRE_TIME,text)

        # 3. 返回图片
        return  HttpResponse(image,content_type='image/jpeg')
        return  HttpResponse(image,content_type='text/html')

# APIView
#GenericAPIView

#ListAPIView,CreateAPIView

# 因为用到序列化器 可以选项 GenericAPIView
# 当然用 APIView也可以
class RegisterSmsCodeView(GenericAPIView):

    """
    GET     /verifications/?moble=xxx&text=xxx&uuid=xxxx
    GET     /verifications/moble/text/uuid


    # 采用
    GET     /verifications/smscodes/(?P<mobile>1[345789]\d{9})/?text=xxxx & image_code_id=xxxx

    # 1. 手机号, text,uuid
    # 2. 我需要对 text 和 uuid进行校验
    # 3. 生成手机短信验证码
    # 4. 发送

    """
    serializer_class = RegisterSmsCodeSerializer

    def get(self,request,mobile):
        # 1. 手机号, text,uuid
        params = request.query_params
        # 2. 我需要对 text 和 uuid进行校验
        # serializer = RegisterSmsCodeSerializer(data=params)
        serializer = self.get_serializer(data=params)
        serializer.is_valid(raise_exception=True)
        # 3. 生成手机短信验证码

        sms_code = '%06d'%randint(0,999999)

        # 3.1 将 短信保存到redis中
        redis_conn = get_redis_connection('code')

        # 3.2 先判断用户的这个手机号,之前是否发送过短信
        flag = redis_conn.get('sms_flag_%s'%mobile)
        if flag:
            return Response(status=status.HTTP_429_TOO_MANY_REQUESTS)

        #3.3 记录发送信息
        redis_conn.setex('sms_%s'%mobile,60*5,sms_code)
        # 最好添加一个 标记,来记录 我们给这个手机号发送过短信
        redis_conn.setex('sms_flag_%s'%mobile,60,1)


        # 4. 发送
        # ccp = CCP()
        # ccp.send_template_sms(mobile,[sms_code,5],1)

        from celery_tasks.sms.tasks import send_sms_code

        # 注意点: 我们的任务需要通过 delay 调用
        #       delay 方法的参数是通过delay区传递
        send_sms_code.delay(mobile,sms_code)

        return Response({'message':'ok'})







